<?php
class Qtpl{
	static function assign($params=array()){
		$_tpl = array();
		foreach ($params as $key => $val) {
			if ($key != '') {
				$_tpl[$key] = $val;
			}
		}
		return $_tpl;
	}
	
	/**
	 * 创建url
	 * @param string $ac 控制器名称
	 * @param string $do 方法名称
	 * @param array $params get参数
	 * @param string $domain 由常量控制
	 * @return string
	 */
	static function createUrl($ac, $do, $params = array(), $domain=''){
		switch ($domain){
			case 'admin':
				$url = core_lib_constant::BOSS_URL;
				break;
			default:
				$url = core_lib_constant::MAIN_URL;
				break;
		}
		if(core_lib_constant::REWRITE){
			$url .= "/{$ac}/{$do}.html";
			$qstr = '?';
		}else{
			$url .= "?ac={$ac}&do={$do}";
			$qstr = '&';
		}
		if($params){
			foreach ($params as $k=>$v){
				$qstr .= "{$k}={$v}&";
			}
			$qstr = rtrim($qstr,'&');
			$url = $url.$qstr;
		}
		return $url;
	}
	
	/**
	 * 创建分页
	 * @param int $count 总记录数
	 * @param int $limit 单页数
	 * @param int $i_page 当前页
	 * @param string $style
	 * @param string $domain
	 */
	function paged($count, $limit, $i_page, $ac='', $do='', $style = 'style', $domain = "") {
		$_GET = Qutil::filter($_GET);
		$ac = $ac ? $ac : $_GET['ac'];
		$do = $do ? $do : $_GET['do'];
		unset($_GET['ac']);
		unset($_GET['do']);
		$get = $_GET;
		$pagenum = ceil($count / $limit);		
		$page = min($pagenum, $i_page);
		$prepg = $page - 1;
		$nextpg = $page == $pagenum ? 0 : $page + 1;
		$offset = ($page - 1) * $limit;
		$startdata = $count ? ($offset + 1) : 0;
		$enddata = min($offset + $limit, $count);
		if ($pagenum > core_lib_constant::PAGE_NUM) {
			if ($page >= core_lib_constant::PAGE_NUM) {
				$pageStart = $page - core_lib_constant::PAGE_NUM / 2;
				if ($pagenum - $pageStart < core_lib_constant::PAGE_NUM) {
					$pageStart = $pagenum - core_lib_constant::PAGE_NUM;
				}
			} else {
				$pageStart = 0;
			}
			$pageEnd = min(($pageStart + core_lib_constant::PAGE_NUM), $pagenum);
		} else {
			$pageStart = 0;
			$pageEnd = $pagenum;
		}
		$j = 0;
		for ($i = $pageStart; $i < $pageEnd; $i++) {
			$params['pages'][$j]['page'] = $i + 1;
			$params['pages'][$j]['url'] = Qtpl::createUrl($ac, $do, array_merge($get, array('p'=>$i + 1)), $domain);
			$j++;
		}
		$params['first'] = Qtpl::createUrl($ac, $do, array_merge($get, array('p'=>1)), $domain);
		if (!empty($nextpg)) {
			$params['nextpg'] = Qtpl::createUrl($ac, $do, array_merge($get, array('p'=>$nextpg)), $domain);
		} else {
			$params['nextpg'] = null;
		}
		if (!empty($prepg)) {
			$params['prepg'] = Qtpl::createUrl($ac, $do, array_merge($get, array('p'=>$prepg)), $domain);
		} else {
			$params['prepg'] = null;
		}
		$params['currpage'] = $i_page;
		$params["total"] = $pagenum; //总页数
		return $this->render("page/{$style}.php", $params);
	}
	
	function ubb2html($sHtml){
		global $fInit;
		$sHtml = Qutil::filter($sHtml);
		$sHtml=preg_replace("/\r?\n/",'<br />',$sHtml);
		$sHtml=preg_replace('/\[(\/?)(b|u|i)\]/i','<$1$2>',$sHtml);
		$sHtml=preg_replace('/\[color\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/i','<span style="color:$1;">',$sHtml);
		$sHtml=preg_replace('/\[img\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/img\]/i','<a href="$1" target="_blank"><img src="$1" /></a>',$sHtml);
		if(!$fInit){
			function getImg($match){
				$alt=$match[1];$p1=$match[2];$p2=$match[3];$p3=$match[4];$src=$match[5];
				$a=$p3?$p3:(!is_numeric($p1)?$p1:'');
				return '<img src="'.$src.'" alt="'.$alt.'"'.(is_numeric($p1)?' width="'.$p1.'"':'').(is_numeric($p2)?' height="'.$p2.'"':'').($a?' align="'.$a.'"':'').' />';
			}
			function getUrl($match){
		    	$url = $match[1];
		    	return '<a target="_blank" href="'.$url.'">'.$url.'</a>';
		    }
			function getLink($match){
		    	$url = $match[1];
		    	$str = $match[2];
		    	return '<a target="_blank" href="'.$url.'">'.$str.'</a>';
		    }
			function showCode($match){
				$match[1]=strtolower($match[1]);
				if(!$match[1])$match[1]='plain';
				$match[2]=preg_replace("/</",'&lt;',$match[2]);
				$match[2]=preg_replace("/>/",'&gt;',$match[2]);
				return '<pre name="code" class="'.$match[1].'">'.$match[2].'</pre>';
			}
			function getVideo($match){
				$w=$match[1];$h=$match[2];$url=$match[3];
				if(!$w)$w=480;if(!$h)$h=400;
				return '<embed type="application/x-shockwave-flash" src="'.$url.'" wmode="opaque" quality="high" bgcolor="#ffffff" menu="false" play="true" loop="true" width="'.$w.'" height="'.$h.'" />';
			}
		}
		$sHtml=preg_replace_callback('/\[img\s*=([^,\]]*)(?:\s*,\s*(\d*%?)\s*,\s*(\d*%?)\s*)?(?:,?\s*(\w+))?\s*\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*)?\s*\[\/img\]/i','getImg',$sHtml);
	    $sHtml=preg_replace_callback('/\[url\]\s*(((?!")[\s\S])*?)(?:"[\s\S]*?)?\s*\[\/url\]/i','getUrl',$sHtml);
		$sHtml=preg_replace_callback('/\[url\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]\s*([\s\S]*?)\s*\[\/url\]/i','getLink',$sHtml);
		$sHtml=preg_replace_callback('/\[code\s*(?:=\s*((?:(?!")[\s\S])+?)(?:"[\s\S]*?)?)?\]([\s\S]*?)\[\/code\]/i','showCode',$sHtml);
		$sHtml=preg_replace_callback('/\[video\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/video\]/i','getVideo',$sHtml);
		$fInit = true;
		return $sHtml;		
	}
	
	static function getLevel($rank){
		if($rank == 0) return '无';
		if($rank <= 5){
			return '低';
		}elseif($rank <= 10){
			return '中';
		}elseif($rank <= 20){
			return '高';
		}else{
			return false;
		}
	}
}